/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.server.startup;

import cn.ac.iie.di.commons.httpserver.framework.server.HttpServer;
import cn.ac.iie.di.wx.server.common.RuntimeEnv;
import static cn.ac.iie.di.wx.server.common.RuntimeEnv.redisVos;
import static cn.ac.iie.di.wx.server.common.RuntimeEnv.retryQueue;
import cn.ac.iie.di.wx.server.configuration.Configuration;
import cn.ac.iie.di.wx.server.handler.AddUnicontrolHandler;
import cn.ac.iie.di.wx.server.handler.AddUserFriendCircleMonitorByUserIdHandler;
import cn.ac.iie.di.wx.server.handler.AddUserFriendCircleMonitorHandler;
import cn.ac.iie.di.wx.server.handler.DelUnicontrolHandler;
import cn.ac.iie.di.wx.server.handler.DelUserFriendCircleMonitorByUserIdHandler;
import cn.ac.iie.di.wx.server.handler.DelUserFriendCircleMonitorHandler;
import cn.ac.iie.di.wx.server.handler.OCRHandler;
import cn.ac.iie.di.wx.server.handler.OCR_ThemesHandler;
import cn.ac.iie.di.wx.server.handler.QueryFCMsgByKeywordHandler;
import cn.ac.iie.di.wx.server.handler.QueryFriendCircleMsgHandler;
import cn.ac.iie.di.wx.server.handler.QueryPicByPHandler;
import cn.ac.iie.di.wx.server.killHandler.KillHandler;
import cn.ac.iie.di.wx.server.oracleservice.UpdateOracleThread;
import cn.ac.iie.di.wx.server.redisservice.JedisThread;
import cn.ac.iie.di.wx.server.redisservice.RedisThread;
import cn.ac.iie.di.wx.server.timerTask.CountTask;
import cn.ac.iie.di.wx.server.timerTask.RetryTask;
import cn.ac.iie.di.wx.server.utils.RedisUtil;
import java.util.Timer;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class Starter {

    static Logger logger = Logger.getLogger(Starter.class.getName());
    static HttpServer httpServer = null;

    public static void main(String[] args) {
        try {
            init();
            logger.info(RuntimeEnv.getParam(RuntimeEnv.IP));
            logger.info(RuntimeEnv.getParam(RuntimeEnv.PORT));
            logger.info((String) RuntimeEnv.getParam(RuntimeEnv.addUserFriendCircleMonitor));
            oracleUpdateThread();
            CountTask countTask = new CountTask();
            Timer timer = new Timer();
            timer.schedule(countTask, 1000, 1000);
            KillHandler killHandler = new KillHandler();
            killHandler.registerSignal("TERM");
            killHandler.putQueue("redisQueue", RuntimeEnv.oracleUpdateQueue);
            killHandler.putQueue("redisVos", RuntimeEnv.redisVos);
            redisThreadStartup();
            retryRedisThreadStartup();
            startup();
            logger.info("start server at " + RuntimeEnv.getParam(RuntimeEnv.IP) + ":" + RuntimeEnv.getParam(RuntimeEnv.PORT) + " success.");
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }

    }

    public static void init() throws Exception {
        String configurationFileName = "configuration.properties";
        Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource(configurationFileName).getPath());
        if (conf == null) {
            logger.error("reading " + configurationFileName + " is failed.");
        }
        try {
            RuntimeEnv.initialize(conf);///参数，
            logger.info("init successfully");
        } catch (Exception ex) {
            logger.error("initializng runtime enviroment is failed for" + ex.getMessage());
        }
    }

    public static void startup() {
        try {
            httpServer = HttpServer.getHttpServer(
                    (String) RuntimeEnv.getParam(RuntimeEnv.IP),
                    Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.PORT)),
                    Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.THREADNUM)));
            httpServer.registerContext("/service");
            httpServer.registerContextHandler("/service", "/addUnicontrol", AddUnicontrolHandler.class);//本地布控
            httpServer.registerContextHandler("/service", "/delUnicontrol", DelUnicontrolHandler.class);//本地停控
            httpServer.registerContextHandler("/service", "/addUserFriendCircleMonitor", AddUserFriendCircleMonitorHandler.class);//朋友圈布控
            httpServer.registerContextHandler("/service", "/addUserFriendCircleMonitorByUserId", AddUserFriendCircleMonitorByUserIdHandler.class);//朋友圈布控，带userid和accountname
            httpServer.registerContextHandler("/service", "/delUserFriendCircleMonitor", DelUserFriendCircleMonitorHandler.class);//朋友圈停控
            httpServer.registerContextHandler("/service", "/delUserFriendCircleMonitorByUserId", DelUserFriendCircleMonitorByUserIdHandler.class);//朋友圈停控，带userid和accountname
            httpServer.registerContextHandler("/service", "/queryOCR", OCRHandler.class);//境外用户注册信息查询
            httpServer.registerContextHandler("/service", "/queryPicByP", QueryPicByPHandler.class);//群注册信息查询
            httpServer.registerContextHandler("/service", "/queryFriendCircleMsg", QueryFriendCircleMsgHandler.class);//3.1.4	朋友圈消息查询
            httpServer.registerContextHandler("/service", "/queryFriendCircleMsgByUserId", QueryFriendCircleMsgHandler.class);//朋友圈消息查询,通过用户id进行查询
            httpServer.registerContextHandler("/service", "/queryFCMsgByKeyword", QueryFCMsgByKeywordHandler.class);//3.1.4	朋友圈消息查询
            httpServer.registerContextHandler("/service", "/queryOCRTheme", OCR_ThemesHandler.class);//3.1.4	朋友圈消息查询
//            httpServer.registerContextHandler("/service", "/queryOCRTopics", OCR_TopicsHandler.class);//3.1.4	朋友圈消息查询

            httpServer.startup();
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }

    }

    public static void redisThreadStartup() {
        JedisThread jedisThread = new JedisThread(RuntimeEnv.redisVos);
        Thread thread = new Thread(jedisThread);
        thread.start();
//        RedisUtil redisUtil = new RedisUtil((String) RuntimeEnv.getParam(RuntimeEnv.redis_hosts),
//                (String) RuntimeEnv.getParam(RuntimeEnv.redis_masterName),
//                Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.redis_dbName)));
//        RedisThread redisThread = new RedisThread(redisUtil, redisVos);
//        Thread t = new Thread(redisThread);
//        t.start();
        logger.info("redis Thread Startup");
    }

    public static void oracleUpdateThread() {
        UpdateOracleThread oracleThread = new UpdateOracleThread();
        Thread thread = new Thread(oracleThread);
        thread.start();
    }

    public static void retryRedisThreadStartup() {

        RedisUtil redisUtil = new RedisUtil((String) RuntimeEnv.getParam(RuntimeEnv.redis_hosts),
                (String) RuntimeEnv.getParam(RuntimeEnv.redis_masterName),
                Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.redis_dbName)));
        RetryTask retryTask = new RetryTask(redisUtil, retryQueue);
        Thread t = new Thread(retryTask);
        t.start();
    }
}
